import { defineStore } from "pinia";
import { getPermissions, getMenus } from "@/api/login";
import { listToTagsViewTree } from "@/utils";
import { constantRoutes } from "@/router/index";

export const usePermissionStore = defineStore("permission", {
  state: () => ({
    routes: constantRoutes,
    allMenus: [] as SysMenuEntity[],
    menus: [] as tabView[],
    permissions: [] as string[],
  }),
  actions: {
    //获取菜单信息
    async getAllMenus() {
      const res: apiServerResponse = await getMenus();
      if (res.code == 200) {
        this.allMenus = res.result;
        const listMenus = this.allMenus.filter(
          (item) => item.status == "1" && item.menuType != "2"
        );
        this.menus = listToTagsViewTree(listMenus);
      }
    },
    //获取权限信息
    getPermissions() {
      getPermissions().then((res: apiServerResponseType) => {
        if (res.code == 200) {
          this.permissions = res.result;
        }
      });
    },
  },
});

export default usePermissionStore;
